"""
给定一个单链表 L：L0→L1→…→Ln-1→Ln ，
将其重新排列后变为： L0→Ln→L1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值，而是需要实际的进行节点交换。

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.
示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/reorder-list
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        if not head:
            return None

        # 寻找链表中点
        fast = head
        slow = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        # 截断链表后半部分并反转
        pre = None
        cur = slow.next
        slow.next = None
        while cur:
            cur.next, cur, pre = pre, cur.next, cur

        # 拼接两条链表
        insert = pre
        cur = head
        while cur and insert:
            tmp1 = cur.next
            cur.next = None
            tmp2 = insert.next
            insert.next = None
            cur.next = insert
            cur.next.next = tmp1
            cur = tmp1
            insert = tmp2
